/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package codemanager.widget;

import java.io.File;
import javax.swing.JTree;
import javax.swing.tree.DefaultMutableTreeNode;

/**
 *display a tree-like view that show user's projects and there subfiles 
 * @author undefined
 */
public class FileTreeView{
    //tree
    private JTree tree;
    //root node of tree
    private DefaultMutableTreeNode root;
    //constructor
    public FileTreeView(String title) {
        if(null==root){
            root = new DefaultMutableTreeNode(title);
        }
        initRootNodes();
         //if instance of tree doesn't exist, then create a instance for it
        if(tree==null){
            tree = new JTree(root);
        }       
    }
    
    private void initRootNodes(){
        DefaultMutableTreeNode proRootNode = new DefaultMutableTreeNode("我的项目");
        DefaultMutableTreeNode pluginRootNode = new DefaultMutableTreeNode("常用插件");
        root.add(proRootNode);
        root.add(pluginRootNode);
    }
    //
    public JTree addMyProject(String filePath){
        File file = new File(filePath);
        DefaultMutableTreeNode proNode = new DefaultMutableTreeNode(file.getName());
        generateFileTreeView(proNode,filePath);
        ((DefaultMutableTreeNode)(root.getChildAt(0))).add(proNode);
        return tree;
    } 
    
    private void generateFileTreeView(DefaultMutableTreeNode parent, String path){
        File file = new File(path);
        File []subFiles;
        subFiles= file.listFiles();
        for(int i=0;i<subFiles.length;i++){
            if(!subFiles[i].getName().startsWith(".")){
             DefaultMutableTreeNode subNode = new DefaultMutableTreeNode(subFiles[i].getName());
             parent.add(subNode);
             if(subFiles[i].isDirectory()){
                 generateFileTreeView(subNode,subFiles[i].getAbsolutePath());
             }
            }
        }
    }
}
